<?php

/**
 * Desc:
 * User: tb
 * Date: 2016/8/15
 * Time: 18:36
 */
final class Base_Route implements Yaf_Route_Interface
{

    public function route($request)
    {

        $this->_setRewriteRule($request);
        // setRouted是Yaf_Request_Http::setRouted的方法
        $request->setRouted();
         return true;
    }

    public function _setRewriteRule(Yaf_Request_Abstract $request)
    {

        $uri = $request->getRequestUri();//uri 为string(17) "/index/index/show"
        $parse = explode('/', trim($uri, '/'));
        $config=Yaf_Registry::get('config')->get('application');
        //判断是否多模块
        $modules=($modules=$config->get('modules'))?explode(',',$modules):[];

        $module=$controller=$action=null;
        //注意模块在声明的时候要大写
        $module=$modules&&in_array(($parse[0]),$modules )?array_shift($parse):null;

        $action=count($parse)>1?array_pop($parse):null;

        // 把module 和action 剔除后，处理下controlelr
        $controller=empty($parse)?null:implode('_',$parse );

        Yaf_Registry::set('_route_action',$action);//aciton肯定是最后一个，pop出来的

        $action and $action =str_replace('_', '',$action );//不会有action加——的吧，

        $module or $module = 'Index'; //默认就是Index模块
        $controller or $controller = 'Common';
        $action or $action = 'index';

        $request->setModuleName($module);
        $request->setControllerName($controller);
        $request->setActionName($action);
        echo '<hr>';
        var_dump($module,$controller,$action);
        echo '<hr>';

    }


}